import { http } from '@/utils/http'
import type { CartItem } from '@/types/cart'

/**
 * 加入购物车
 * @param { string } skuId sku id
 * @param { number } count 数量
 */
export const addCartAPI = (skuId: string, count: number) => {
  return http({
    url: '/member/cart',
    method: 'POST',
    data: {
      skuId,
      count,
    },
  })
}

/**获取购物车列表 */
export const getCartListAPI = () => {
  return http<CartItem[]>({
    url: '/member/cart',
    method: 'GET',
  })
}

/**删除/清空购物车单品
 * @param { string[] } ids SKUID集合
 */
export const deleteCartAPI = (ids: string[]) => {
  return http({
    url: '/member/cart',
    method: 'DELETE',
    data: {
      ids,
    },
  })
}

/**
 * 修改购物车单品
 * @param { string } skuId SKU ID
 * @param { object } data  selected 选中状态 count 商品数量
 */
export const updateCartAPI = (skuId: string, data: { selected?: boolean; count?: number }) => {
  return http<CartItem>({
    url: `/member/cart/${skuId}`,
    method: 'PUT',
    data,
  })
}

/**
 * 购物车全选/反选
 * @param { boolean } selected 是否选中
 */
export const selectAllAPI = (selected: boolean) => {
  return http({
    url: '/member/cart/selected',
    method: 'PUT',
    data: {
      selected,
    },
  })
}
